package com.mchat;

import com.mchat.ratelimit.aspect.RateLimitAspect;
import com.mchat.util.MailUtil;
import com.mchat.util.SnowflakeIdGenerator;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.mail.javamail.JavaMailSender;

@Configuration
@EnableConfigurationProperties({CommonProperties.class})
@AutoConfiguration
public class CommonAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean
    public SnowflakeIdGenerator snowflakeIdGenerator(CommonProperties commonProperties){

        return new SnowflakeIdGenerator(commonProperties.getSnowflake().getWorkerId(),commonProperties.getSnowflake().getDataCenterId());
    }
    @Bean
    @ConditionalOnMissingBean
    public MailUtil mailUtil(CommonProperties commonProperties, JavaMailSender javaMailSender){

        return new MailUtil(commonProperties.getUsername(),commonProperties.getMailValidTime(),javaMailSender);
    }
    @Bean
    @ConditionalOnMissingBean
    public RateLimitAspect rateLimitAspect(){
        return new RateLimitAspect();
    }

}
